import pygame
import random
import sys

# 初始化pygame
pygame.init()

# 屏幕尺寸
WIDTH, HEIGHT = 800, 600
# screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Happy Mrs. Chicken")

# 颜色定义
BACKGROUND_COLOR = (72, 201, 246)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BUTTON_COLOR = (0, 200, 0)
BUTTON_HOVER_COLOR = (0, 255, 0)

# 加载图片
hen_images = [pygame.image.load(f"hen_{i}.png") for i in range(1, 3)]
egg_images = [pygame.image.load(f"egg_{i}.png") for i in range(1, 4)]  # 鸡蛋孵化动画的三张图片
chick_images = [pygame.image.load(f"chick_{i}.png") for i in range(1, 3)]

HEN_SIZE = 100
EGG_SIZE = 50
CHICK_SIZE = 50

JUMP_SPEED = 5
JUMP_HIGHEST = 50
# 缩放图片
hen_images = [pygame.transform.scale(img, (HEN_SIZE, HEN_SIZE)) for img in hen_images]
egg_images = [pygame.transform.scale(img, (EGG_SIZE, EGG_SIZE)) for img in egg_images]
chick_images = [
    pygame.transform.scale(img, (CHICK_SIZE, CHICK_SIZE)) for img in chick_images
]
chick_flip_images = [pygame.transform.flip(img, True, False) for img in chick_images]

pygame.mixer.init()
background_music = pygame.mixer.Sound("background_music.mp3")
buk_music = pygame.mixer.Sound("buk.mp3")
chirp_music = pygame.mixer.Sound("chirp.mp3")
background_music.play(-1)


# 母鸡类
class Hen:
    def __init__(self):
        self.x = WIDTH // 2
        self.y = HEIGHT // 2
        self.current_y = 0
        self.images = hen_images
        self.current_image = 0
        self.jumping = False
        self.jump_height = 0
        self.laying = False
        self.landing = False
        self.eggs = []

    def is_idle(self):
        # if self.landing or self.jumping or self.laying:
        #     return False
        return True

    def jump(self):
        self.current_y = self.y
        self.jump_height = self.current_y - JUMP_HIGHEST
        self.jumping = True

    def update(self):
        # current_time = pygame.time.get_ticks()
        # if current_time - self.last_action_time > 5000:  # 5秒空闲
        #     self.jump()
        #     self.last_action_time = current_time

        if self.jumping:
            if self.y > self.jump_height:
                self.y -= JUMP_SPEED
            else:
                self.jumping = False
                self.landing = True
                self.current_image = 1
                if self.laying:
                    self.laying = False

        if self.landing:
            self.y += JUMP_SPEED
            if self.y >= self.current_y:
                self.current_image = 0
                self.landing = False

    def lay(self):
        self.x = random.randint(0, WIDTH - HEN_SIZE)
        self.y = random.randint(JUMP_HIGHEST, HEIGHT - HEN_SIZE)
        self.laying = True
        self.jump()
        self.eggs.append(
            Egg(
                self.x + (HEN_SIZE - EGG_SIZE) / 2,
                self.current_y + HEN_SIZE / 2,
            )
        )
        buk_music.play()

    def draw(self, screen):
        screen.blit(self.images[self.current_image], (self.x, self.y))


# 鸡蛋类
class Egg:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.images = egg_images
        self.current_image = 0
        self.hatch_time = pygame.time.get_ticks() + 900  # 1秒后孵化
        self.hatched = False
        self.animation_speed = 300  # 每200毫秒切换一张图片
        self.last_update_time = pygame.time.get_ticks()

    def update(self):
        current_time = pygame.time.get_ticks()
        if current_time > self.hatch_time and not self.hatched:
            self.hatched = True
            return Chick(self.x, self.y)

        # 更新孵化动画
        if current_time - self.last_update_time > self.animation_speed:
            self.current_image = (self.current_image + 1) % len(self.images)
            self.last_update_time = current_time

        return None

    def draw(self, screen):
        if not self.hatched:
            screen.blit(self.images[self.current_image], (self.x, self.y))


# 小鸡类
class Chick:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.images = chick_images
        self.current_image = 0
        self.direction = random.choice([-1, 1])  # 向左或向右
        self.speed = 1
        self.animation_speed = 200  # 每200毫秒切换一张图片
        self.last_update_time = pygame.time.get_ticks()

    def update(self):
        self.x += self.direction * self.speed

        # 向右
        if self.direction == 1:
            self.images = chick_flip_images

        # 更新动画
        current_time = pygame.time.get_ticks()
        if current_time - self.last_update_time > self.animation_speed:
            self.current_image = (self.current_image + 1) % len(self.images)
            self.last_update_time = current_time

    def draw(self, screen):
        screen.blit(self.images[self.current_image], (self.x, self.y))

    def is_off_screen(self):
        return self.x < 0 or self.x > WIDTH


# 绘制按钮
def draw_button(screen, text, x, y, width, height, color, hover_color):
    mouse_pos = pygame.mouse.get_pos()
    clicked = pygame.mouse.get_pressed()[0]

    # 检查鼠标是否在按钮上
    if x < mouse_pos[0] < x + width and y < mouse_pos[1] < y + height:
        pygame.draw.rect(screen, hover_color, (x, y, width, height))
        if clicked:
            return True
    else:
        pygame.draw.rect(screen, color, (x, y, width, height))

    # 添加文字
    font = pygame.font.SysFont("Arial", 36)
    text_surface = font.render(text, True, WHITE)
    text_rect = text_surface.get_rect(center=(x + width // 2, y + height // 2))
    screen.blit(text_surface, text_rect)
    return False


# 游戏主循环
def main():
    clock = pygame.time.Clock()
    hen = Hen()
    chicks = []
    game_started = False
    score = 0

    while True:
        screen.fill(BACKGROUND_COLOR)

        for event in pygame.event.get():
            if (
                event.type == pygame.QUIT
                or event.type == pygame.KEYDOWN
                and event.key == pygame.K_ESCAPE
            ):
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN and game_started:
                hen.lay()
                score += 1

        if game_started:
            # background_music.stop()
            hen.update()
            hen.draw(screen)

            for egg in hen.eggs:
                chick = egg.update()
                if chick:
                    chicks.append(chick)
                    chirp_music.play()
                    hen.eggs.remove(egg)
                egg.draw(screen)

            for chick in chicks:
                chick.update()
                chick.draw(screen)
                if chick.is_off_screen():
                    chicks.remove(chick)

            # 显示得分
            font = pygame.font.SysFont("Arial", 36)
            score_text = font.render(f"Score: {score}", True, BLACK)
            screen.blit(score_text, (10, 10))
        else:
            # 绘制开始按钮
            if draw_button(
                screen,
                "Start",
                WIDTH // 2 - 100,
                HEIGHT // 2 - 50,
                200,
                100,
                BUTTON_COLOR,
                BUTTON_HOVER_COLOR,
            ):
                game_started = True

        pygame.display.flip()
        clock.tick(30)


if __name__ == "__main__":
    main()
